package im.vinci.server.user.persistence;

import im.vinci.server.user.domain.UserAddressBook;
import im.vinci.server.user.domain.UserAttention;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by tim@vinci on 2017/6/26.
 */
@Repository
public interface UserAddressBookMapper {
    @Select("select * from address_book where uid=#{uid}")
    List<UserAddressBook> getUserAddressBook(@Param("uid") long uid);

    @Select("select count(*) from address_book where uid=#{uid}")
    int countUserAddressBook(@Param("uid") long uid);


    @Insert({"<script>",
            "INSERT ignore INTO address_book ",
            "(uid, name, phone_num) VALUES ",
            "<foreach item='item' index='index' collection='list' separator=','>",
            "(#{uid},#{item.name}, #{item.phoneNum})",
            "</foreach>",
            "</script>"})
    int batchInsert(@Param("uid") long uid, @Param("list") List<UserAddressBook> list);

    @Delete("delete from address_book where uid=#{uid}")
    int delete(@Param("uid")long uid);

    @Select({"<script>",
            "select u.id,a.name from (select name,phone_num from address_book where uid=#{uid}) as a inner join real_user_info as u on a.phone_num = u.external_source_uid where u.login_source = 'phone'",
            "</script>"})
    List<Map> getFriendsInAddressBook(@Param("uid")long uid);

    @Insert("insert ignore into v_new_friend_msg " +
            "(user_id, attention_user_id, dt_create, dt_update) " +
            "values " +
            "(#{attention_uid}, #{user_id}, now(),now())")
    boolean addNewFriendMsg(@Param("user_id") long uid , @Param("attention_uid") long attentionUid);

    @Select("select * from v_new_friend_msg where user_id=#{uid} order by id desc")
    List<UserAttention> getNewFriendMsgList(@Param("uid")long uid);
}
